package sz.szsmk.citizencard.ui.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import androidx.fragment.app.FragmentActivity;

import sz.szsmk.citizencard.log.L;

import java.util.Stack;


/**
 * 应用程序Activity管理类：用于Activity管理和应用程序退出
 */
public class AppManager {

	private static Stack<FragmentActivity> activityStack;
	private static AppManager instance;

	private AppManager(){}
	/**
	 * 单一实例
	 */
	public static AppManager getAppManager(){
		if(instance==null){
			instance=new AppManager();
		}
		if(activityStack==null){
			activityStack=new Stack<FragmentActivity>();
		}
		return instance;
	}
	/**
	 * 添加Activity到堆栈
	 */
	public void addActivity(FragmentActivity activity){

		activityStack.add(activity);
		L.e("addActivity", "addActivity:" + activityStack.size() + "=========activity:" + activity.getLocalClassName());
	}
	/**
	 * 获取当前Activity（堆栈中最后一个压入的）
	 */
	public FragmentActivity currentActivity(){
		FragmentActivity activity= null;
		if(!activityStack.isEmpty()) {
			activity=activityStack.lastElement();
		}
		return activity;
	}

	public void popAllActivityExceptOneAndMe(Class<?> cls, FragmentActivity me) {
		int index = 0;
		System.out.println("popAllActivityExceptOneAndMe   activityStack.size():" + activityStack.size());
		while (activityStack.size() > index) {
			FragmentActivity activity = activityStack.lastElement();
			if (activity == null) {
				break;
			}
			if (activity.getClass().equals(cls)) {
				break;
			}
			if (!activity.getClass().equals(me.getClass())) {
				System.out.println("popAllActivityExceptOneAndMe activityStack.size():" + activity.getClass());
				finishActivity(activity);
			} else {
				System.out.println("popAllActivityExceptOneAndMe activityStack.size():" + me.getClass());
				activityStack.remove(activity);
			}
			index++;
		}
		addActivity(me);
	}

	public void popAllActivityAddMe(FragmentActivity activity) {
		int index = 0;
		while (activityStack.size() > index) {
			FragmentActivity a = currentActivity();
			if (a == null) {
				break;
			}
			if (!activity.getClass() .equals(a.getClass()  )) {
				finishActivity(a);
			} else {
				activityStack.remove(a);
			}
			index++;
		}
		addActivity(activity);

	}
	/**
	 * 结束当前Activity（堆栈中最后一个压入的）
	 */
	public void finishActivity(){
		Activity activity=activityStack.lastElement();
		finishActivity(activity);
	}
	/**
	 * 结束指定的Activity
	 */
	public void finishActivity(Activity activity){
		if(activity!=null){
			L.e("finishActivity=====================1", activity.getLocalClassName());
			activityStack.remove(activity);
			activity.finish();

		}
		L.e("finishActivity", "finishActivity:" + activityStack.size());
	}
	/**
	 * 结束指定类名的Activity
	 */
	public void finishActivity(Class<?> cls){
		for (Activity activity : activityStack) {
			if(activity.getClass().equals(cls) ){
				L.e("Activity=======", activity.getLocalClassName());
				finishActivity(activity);
			}
		}
	}
	/**
	 * 结束所有Activity
	 */
	public void finishAllActivity(Context context){
		for (int i = 0, size = activityStack.size(); i < size; i++){
			if (null != activityStack.get(i)){
				L.e("finishAllActivity====", "======================="+activityStack.get(i).getClass().getSimpleName());
				activityStack.get(i).finish();
			}
		}
		activityStack.clear();
		ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		activityMgr.restartPackage(context.getPackageName());
		android.os.Process.killProcess(android.os.Process.myPid());
		System.exit(0);
	}
	public int getActivitySize(){
		return activityStack == null ? 0 : activityStack.size();
	}
	/**
	 * 退出应用程序
	 */
	public void AppExit(Context context) {
		finishAllActivity(context);
	}
}